package 动态数组.队列;

import 动态数组.队列.接口.Queue;

import java.util.Iterator;

/**
 * 循环队列
 * @param <E>
 */
public class ArrayLoopQueue<E> implements Queue<E> {
    private static int DEFAULT_CAPACITY = 10;
    private E[] data;
    //双端队列的队头指针
    private int front;
    //双端队列的队尾指针
    private int rear;
    //队列的实际元素的长度
    private int size;

    public ArrayLoopQueue(){
        //初始化队列长度为默认的容量加1，加1作为front和rear的中间null值用做判队列是否已经满了
        data = (E[]) new Object[DEFAULT_CAPACITY + 1];
        front = 0;
        rear = 0;
        size = 0;
    }
    @Override
    public int size() {
        return size;
    }

    @Override
    public boolean isEmpty() {
        return front == rear;
    }

    @Override
    public void offer(E element) {
        if ((front + 1) % data.length == rear){
            //扩容为当前容量的2倍减1有一个预留为null判满
            resize(data.length * 2 - 1);
        }
        data[rear] = element;
        //队尾做循环的遍历，仅是对元素的赋值操作，之前的元素还是存在的
        rear = (rear + 1) % data.length;
        size++;
    }

    private void resize(int newLen) {
        E[] newData = (E[]) new Object[newLen];
        int index = 0;
        for (int i = front; i != rear; i = (i + 1) % data.length) {
            newData[index++] = data[i];
        }
        data = newData;
        front = 0;
        rear = index;
    }

    @Override
    public E poll() {
        if (isEmpty()){
            return null;
        }
        E ret = data[front];
        front = (front + 1) % data.length;
        size--;
        if (size == (data.length - 1) / 4 && data.length - 1 > DEFAULT_CAPACITY){
            //缩容为默认容量的/2加1，有一个预留的null
            resize(data.length / 2 + 1);
        }
        return ret;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null){
            return false;
        }
        if (this == obj){
            return true;
        }
        if (getClass() != obj.getClass()){
            return false;
        }
        ArrayLoopQueue other = (ArrayLoopQueue) obj;
        if (size != other.size){
            return false;
        }else {
            int j = other.front;
            for (int i = front; i != rear; i = (i + 1) % data.length) {
                if(!data[i].equals(other.data[j])){
                    return false;
                }
                j = (j + 1) % other.data.length;
            }
            return true;
        }
    }

    @Override
    public E element() {
        return data[front];
    }

    @Override
    public void clear() {
        data = (E[]) new Object[DEFAULT_CAPACITY + 1];
        front = 0;
        rear = 0;
        size =0;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append('[');
        if (isEmpty()){
            sb.append(']');
        }else {
            for (int i = front; i != rear; i = (i + 1) % data.length) {
                sb.append(data[i]);
                if ((i + 1) % data.length == rear){
                    sb.append(']');
                }else {
                    sb.append(',');
                }
            }
        }
        return sb.toString();
    }

    @Override
    public Iterator<E> iterator() {
        return new ArrayLoopQueueIterator();
    }

    class ArrayLoopQueueIterator implements Iterator<E>{
        private int cur = front;

        @Override
        public boolean hasNext() {
            return front != rear;
        }

        @Override
        public E next() {
            E ret = data[front];
            front = (front + 1) % data.length;
            return ret;
        }
    }
}
